package com.c2b.algorithm.leetcode.jzoffer;

/**
 * 反转链表
 * <p>
 * 给定一个单链表的头结点pHead(该头节点是有值的，比如在下图，它的val是1)，长度为n，反转该链表后，返回新链表的表头。
 * </p>
 *
 * @author c2b
 * @since 2023/3/7 14:46
 */
public class JzOffer0024ReverseList_S {
    /**
     * 双指针
     */
    public ListNode ReverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode prevNode = null;
        ListNode currNode = head;
        ListNode nextNode;
        while (currNode != null) {
            nextNode = currNode.next;
            currNode.next = prevNode;
            prevNode = currNode;
            currNode = nextNode;
        }
        return prevNode;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);

        JzOffer0024ReverseList_S jzOffer0024ReverseList = new JzOffer0024ReverseList_S();
        ListNode listNode = jzOffer0024ReverseList.ReverseList(head);
        System.out.println();

    }
}
